<?php
namespace AiSPRDocManager\Model;

use AiSPRDocManager\Model\Db as AiCMModelDb;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Validate
 *
 * @author Asfer <rmasfer@gmail.com>
 */
class Cv {

	private $_aErrors = array();

	private $_bValid = true;

	public function isValid ($aPost, $aFile = null) {

		$aFormElements = array(
			'textName' => array(
				'isEmpty'
			),
			'textEmail' => array(
				'isEmpty',
				'isEmail'
			),
			'textareaComments' => array(
				'isEmpty'
			)
		);

		foreach ($aFormElements as $sElementKey => $aValidationsList) {
			foreach ($aValidationsList as $sKey => $sValidationName) {
				call_user_func(array($this, $sValidationName), $sElementKey, $aPost[$sElementKey]);
			}
		}

		if (!empty($_FILES['fileCv'])) {
			$oFile = $_FILES['fileCv'];
			$aAllowedFileType = array('doc', 'docx');

			$aFileInfo = pathinfo($oFile['name']);

			if (!in_array($aFileInfo['extension'], $aAllowedFileType)) {
				$this->_aErrors['fileCv'][] = 'File type is not allowed';
				$this->_bValid = false;
			}

		} else {

			$this->_aErrors['fileCv'][] = 'Field is empty';
			$this->_bValid = false;
		}

//		var_dump($this->_bValid);

		return $this->_bValid;
	}

	public function action () {
		$aContent = array();
		$sNotice = '';
		$aCvList = array();

		$oDbCvManager = new AiCMModelDb\CvManager();
		$mCvList = $oDbCvManager->fetchAll(Utility::getTableName('cv_manager'));

		if ($mCvList != '') {
			if (!empty($mCvList)) {
				$aCvList = $mCvList;
			}
		} else {
			$sNotice = $mCvList;
		}

		$aContent['notice'] = $sNotice;
		$aContent['cvList'] = $aCvList;

		return $aContent;
	}
	private function test () {
		die('test');
	}

	public function getErrors () {
		return $this->_aErrors;
	}

	public function getBValid () {
		return $this->_bValid;
	}

	private function isEmpty ($sElementName, $sValue) {

		if ( empty($sValue) || is_null($sValue) || !(isset($sValue))) {
			$this->_aErrors[$sElementName][] = 'Field is empty';
			$this->_bValid = false;
		}

		return $this->_bValid;
	}

	private function isInt ($sElementName, $mValue) {

		if (!is_numeric($mValue)) {
			$this->_aErrors[$sElementName][] = 'Entered value is not integer';
			$this->_bValid = false;
		}

		return $this->_bValid;
	}


	private function alphaNumeric ($sElementName, $mValue) {

		if (!ctype_alnum($mValue)) {
			$this->_aErrors[$sElementName][] = 'Entered value is not integer';
			$this->_bValid = false;
		}

		return $this->_bValid;

	}

	private function isEmail ($sElementName, $mValue) {
		if (!is_email($mValue)) {
			$this->_aErrors[$sElementName][] = 'Entered Email address is not valid';
			$this->_bValid = false;
		}

		return $this->_bValid;
	}

}

?>